class FileItem {
  final int id;
  final String filename;
  final int size;
  final String fileType;
  final String filePath;
  final String hash;
  final String createTime;
  final String uploadTime;
  final String extension;
  final String mimeType;
  final bool isDeleted;
  final String? deletedTime;
  final String? remark;
  final int userId;
  final int directoryId;

  FileItem({
    required this.id,
    required this.filename,
    required this.size,
    required this.fileType,
    required this.filePath,
    required this.hash,
    required this.createTime,
    required this.uploadTime,
    required this.extension,
    required this.mimeType,
    required this.isDeleted,
    this.deletedTime,
    this.remark,
    required this.userId,
    this.directoryId = 0,
  });

  factory FileItem.fromJson(Map<String, dynamic> json) {
    return FileItem(
      id: json['id'] as int,
      filename: json['file_name'] as String,
      size: json['file_size'] as int,
      fileType: json['file_type'] as String,
      filePath: json['file_path'] as String,
      hash: json['hash'] as String,
      createTime: json['create_time'] as String,
      uploadTime: json['upload_time'] as String,
      extension: json['extension'] as String,
      mimeType: json['mime_type'] as String,
      isDeleted: json['is_deleted'] as bool? ?? false,
      deletedTime: json['deleted_time'] as String?,
      remark: json['remark'] as String?,
      userId: json['user_id'] as int,
      directoryId: json['directory_id'] as int? ?? 0,
    );
  }

  String formatSize() {
    if (size < 1024) {
      return '$size B';
    } else if (size < 1024 * 1024) {
      return '${(size / 1024).toStringAsFixed(1)} KB';
    } else if (size < 1024 * 1024 * 1024) {
      return '${(size / (1024 * 1024)).toStringAsFixed(1)} MB';
    } else {
      return '${(size / (1024 * 1024 * 1024)).toStringAsFixed(1)} GB';
    }
  }

  String get deletedAt => deletedTime ?? createTime;
}
